package com.wanxi.servlet;

import com.wanxi.model.BrandModel;
import com.wanxi.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/brandUpdate")
public class BrandUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        String enterpriseName = req.getParameter("enterpriseName");
        String sort = req.getParameter("sort");
        String describe = req.getParameter("describe");
        String status = req.getParameter("status");
//        3.封装对象
        BrandModel model = new BrandModel();
        model.setId(Integer.valueOf(id));
        model.setName(name);
        model.setEnterpriseName(enterpriseName);
        model.setSort(Integer.valueOf(sort));
        model.setDescribe(describe);
        model.setStatus(Integer.valueOf(status));
//      4.调用服务逻辑层
        BrandService brandService = new BrandService();
//        以后只要参数多余两个，而且意思差不多，则直接用对象进行传递
//        int res = brandService.add(name, enterpriseName, sort, describe, status);
//      5.得到服务逻辑层的返回结果
        int res = brandService.update(model);
//        6.将返回结果返回给前端页面
//        req.setAttribute("res", res);
        req.getRequestDispatcher("/findAll").forward(req, resp);
    }
}
